extends koa2_srv
extends koa2_db
extends koa2_model
extends koa2_router

snippet rq
	const $1 = require('${2:$1}')
snippet rq}
	const { $1 } = require('${2}')
snippet ip
	import $1 from '${2:$1}'
snippet aw
	await 
snippet as
	async ($1) => {
		$0
	}
snippet as>
	async (ctx, next) => {
		$0
	}
#######################################################################
#                            db_conn_conf                             #
#######################################################################

snippet koa.sql.conf
	export default {
		database: '${2:test}',
		username: 'root',
		password: '$1',
		host: 'localhost',
		port: 3306
	}
snippet koa.sql.conn
	import Sequelize from 'sequelize'
	import config from '../config/custom'

	export default new Sequelize(
	config.db.database,
	config.db.username,
	config.db.password, {
		host: config.db.host,
		dialect: 'mysql',
		pool: {
			max: 5,
			min: 0,
			idle: 30000
		}
	})
snippet koa.use
	app.use(${1:router}.routes(), $1.allowedMethods())
#######################################################################
#                            models define                            #
#######################################################################

snippet model.def
	import db from '../utils/db'
	import Sequelize from 'sequelize'

	export default db.defineModel('${1:modelName}', {
		sql+$0
	})
snippet sql+,
	${1:filedName}: {
		sql+$0
	},
snippet sql+s,
	${1:str}: Sequelize.STRING(${2:100}),$0
snippet sql+b,
	${1:bool}: Sequelize.BOOLEAN,$0
snippet sql+i,
	${1:int}: Sequelize.INTEGER,$0
snippet sql+bi,
	${1:int}: Sequelize.BIGINT,$0
snippet sql+d,
	${1:int}: Sequelize.DECIMAL,$0
snippet sql+e,
	${1:field}: {
		type: Sequelize.ENUM,
		values: ['$2', '$3']
	},$0
snippet sql+s
	${1:str}: Sequelize.STRING(${2:100}),
	sql+$0
snippet sql+b
	${1:bool}: Sequelize.BOOLEAN,
	sql+$0
snippet sql+i
	${1:int}: Sequelize.INTEGER,
	sql+$0
snippet sql+bi
	${1:int}: Sequelize.BIGINT,
	sql+$0
snippet sql+d
	${1:int}: Sequelize.DECIMAL,
	sql+$0
snippet sql+e,
	${1:field}: {
		type: Sequelize.ENUM,
		values: ['$2', '$3']
	},
	sql+$0
#######################################################################
#                           router                                    #
#######################################################################

snippet router.def
	import Router from 'koa-router'
	import $1 from '../services/${1:service}'

	const router = Router()
	router.prefix('/$1')

	router.${2:post}('/${3:add}', async (ctx, next) => {
		await $1.$3(ctx)
		${4:ctx.body = 'ok'}
	})

	router+$0

	export default router
snippet router+
	router.${1:get}('/${2:url}', async (ctx, next) => {
		await $1.$2(ctx)
		${3:ctx.body = 'ok'}
	})
#######################################################################
#                          services                                   #
#######################################################################

snippet srv.def
	import $1 from '../models/${1:modelName}'
	import CODE from '../utils/errcode'

	export default {

		srv.add$0
	}
snippet srv.add
	async add (ctx, next) {
		let body = ctx.request.body
		let json = {
			srv.key${2:+}
		}
		let result = await $1.findAll({
			where: {
				srv.key${3:+}
				status: CODE.ACTIVE_DATA
			}
		})
		if (result.length > 0) {
			ctx.body = { errcode: CODE.FAIL, msg: 'record exist' }
		} else {
			${1:modelName}.create(json)
			ctx.body = { errcode: CODE.SUCCESS, msg: 'add sucess' }
		}
	},$0
snippet srv.del
	async delete (ctx, next) {
		await ${1:modelName}.update({
			status: 1,
			deleteTime: new Date().getTime()
		}, {
			where: {id: ctx.request.body.id}
		})
		ctx.body = { errcode: CODE.SUCCESS, msg: 'delete sucess' }
	}$0
snippet srv.phydel
	async delete (ctx, next) {
		await ${1:modelName}.destroy({
			where: {
				id: ctx.request.body.id
			}
		})
		ctx.body = { errcode: CODE.SUCCESS, msg: 'physical delete sucess' }
	}$0
snippet srv.findall
	async findAll (ctx, next) {
		let result = await ${1:modelName}.findAll({
			where: { status: CODE.ACTIVE_DATA }
		})
		ctx.body = result.length
			? { errcode: CODE.SUCCESS, data: result }
			: { errcode: CODE.FAIL, msg: 'not found' }
	},$0
snippet srv.findby
	async findBy${1:Id} (ctx, next) {
		let body = ctx.request.body
		let result = await ${2:modelName}.findAll({
			where: {
				srv.key$0,
				status: CODE.ACTIVE_DATA
			}
		})
		ctx.body = result.length
			? { errcode: CODE.SUCCESS, data: result }
			: { errcode: CODE.FAIL, msg: 'not found' }
	}
snippet srv.update
	async update (ctx, next) {
		let body = ctx.request.body
		let result = await $1.findAll({
			where: {
				srv.key${2:+}
			}
		})
		if (result.length > 0) {
			await ${1:modelName}.update({
				srv.key${3:+}
				updatedAt: Date.now(),
				version: result[0].version
			}, {
				where: {id: result[0].id}
			})
			ctx.body = { errcode: CODE.SUCCESS, msg: 'update sucess' }
		} else {
			ctx.body = { errcode: CODE.FAIL, msg: 'id not exist' }
		}
	},$0
snippet srv.key
	${1:key}: body.$1$0
snippet srv.key+
	${1:key}: body.$1,
	srv.key+$0
snippet srv.key+p
	${1:key}: body.$2$1,
	srv.key+$0
